//
//  TFAwardView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/5.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TFAwardView: TalkfunHitTestView {
    //旋转的图片
   let awardImage = UIImageView()
   let backgroundImageView = UIImageView()
   let closeButton = UIButton()
    var rotationAnimation:CABasicAnimation?
    var timer:Timer? = nil
    override init(frame: CGRect) {
        super.init(frame: frame)
       
        self.addSubview(self.backgroundImageView)
        
        self.backgroundImageView.image = UIImage.init(named: "HTLive.bundle/JBback")
        self.backgroundImageView.isHidden = true
        
        self.addSubview(self.awardImage)
        self.awardImage.image = UIImage.init(named: "HTLive.bundle/bigjiangbei")
        self.awardImage.isHidden = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        var awardImageW:CGFloat = 168
        var awardImageH:CGFloat = 168
        
        if (UIDevice.current.userInterfaceIdiom == .pad)
        {
             awardImageW = 528*0.6
             awardImageH = 528*0.6
        }
        
        let awardImageX:CGFloat =  (self.frame.size.width - awardImageW)/2
        let awardImageY:CGFloat =  (self.frame.size.height - awardImageH)/2
        
        self.backgroundImageView.frame = CGRect(x: awardImageX, y: awardImageY, width: awardImageW, height: awardImageH)
      
        
        var backgroundImageVieW:CGFloat = 144
        var backgroundImageVieH:CGFloat = 133
        if (UIDevice.current.userInterfaceIdiom == .pad)
        {
            backgroundImageVieW = 302*0.6
             backgroundImageVieH = 237*0.6
            
        }
        let backgroundImageViewX = (self.frame.size.width - backgroundImageVieW)/2
        let backgroundImageViewY = (self.frame.size.height - backgroundImageVieH)/2
        
        self.awardImage.frame = CGRect(x: backgroundImageViewX, y: backgroundImageViewY, width: backgroundImageVieW, height: backgroundImageVieH)
       
    }
    
    @objc public func rotateView()
    {
        
        self.backgroundImageView.layer.removeAllAnimations()//停止动画
        
        self.backgroundImageView.isHidden = false
        self.awardImage.isHidden = false
      

        if(self.rotationAnimation != nil)
        {
            self.rotationAnimation = nil
        }
        
        self.rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
      
        self.rotationAnimation!.toValue = (.pi*3.0)
        self.rotationAnimation!.duration = 3
   
        self.rotationAnimation!.isCumulative = true
        self.rotationAnimation!.repeatCount = 1
        self.rotationAnimation!.isRemovedOnCompletion = false
        
        self.isUserInteractionEnabled = false
        self.backgroundImageView.layer.add(self.rotationAnimation!, forKey: "rotationAnimation")
        if((self.timer) != nil)
        {
            self.timer!.invalidate()
            self.timer = nil
        }
        self.timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.animationDidStop), userInfo: nil, repeats: true)

        RunLoop.main.add(self.timer! , forMode: RunLoop.Mode.default)
      
    }

  @objc public func animationDidStop()
    {
        if((self.timer) != nil)
        {
            self.timer!.invalidate()
            self.timer = nil
        }
        self.backgroundImageView.isHidden = true
        self.awardImage.isHidden = true
    }

}
